package com.jp.knowledge.activity;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.FragMentAdapter;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.my.model.OrganizaModel;
import com.jp.knowledge.view.TabView;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiayen on 17/3/30.
 */

public abstract class CompanyBaseAcytivity extends SlidingActivity implements View.OnClickListener {

    protected UserData userData;
    private List<OrganizaModel> companys;

    private FragMentAdapter adapter;
    /***
     * 通讯录视图
     */
    @ViewInject(R.id.view_page)
    protected ViewPager viewPager;

    /**
     * tab
     **/
    @ViewInject(R.id.tab_view)
    protected TabView tabView;

    @Override
    protected int getResourceId() {
        return R.layout.activity_company_base;
    }

    @Override
    protected void init() {
        userData = application.getUserData();
        if (userData == null) {
            ToasUtil.toast(mContext, "请先登录!");
            finish();
            return;
        }
        companys = getCompanys();
        if (companys == null || companys.size() == 0) {
            ToasUtil.toast(mContext, "尚未加入任何组织!");
            finish();
            return;
        }
        topName.setText(getTitleString());
        rightIcon.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.guanzhufanhui);
        leftIcon.setOnClickListener(this);
        List<Fragment> list = new ArrayList<>();
        int size = companys.size();
        for (int i = 0; i < size; i++) {
            list.add(getFragment(companys.get(i)));
        }
        setTabNames(companys);
        adapter = new FragMentAdapter(getSupportFragmentManager(), list);
        viewPager.setAdapter(adapter);
        tabView.setupWithViewPager(viewPager);
    }

    protected void setTabNames(List<OrganizaModel> companys) {
        int size = companys.size();
        if(size<=1)
            tabView.setVisibility(View.GONE);
        else
            tabView.setVisibility(View.VISIBLE);
        List<String> tabTitles = new ArrayList<>();
        for(OrganizaModel organizaModel : companys)
            tabTitles.add(organizaModel.getCompanyFullName());
        tabView.setTabItems(tabTitles);
    }


    /**
     * 获取公司信息
     * @return
     */
    protected  List<OrganizaModel> getCompanys(){
        List<OrganizaModel> companyList = new ArrayList<>();
        List<OrganizaModel> allCompanyList = userData.getCompanyList();
        if(allCompanyList != null && allCompanyList.size() > 0) {
            for (OrganizaModel company : allCompanyList){
                if(company.getCompanyState() != 1 || (company.getAuditStete() != 0 && company.getAuditStete() != 2))
                    companyList.add(company);
            }
        }
        return  companyList;
    }
    protected abstract String getTitleString();

    protected abstract Fragment getFragment(OrganizaModel company);

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.icon_left:
                onBackPressed();
                break;
            default:
                break;
        }
    }
}
